// DlgCompleteManual.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgCompleteManual.h"

// CDlgCompleteManual dialog

IMPLEMENT_DYNAMIC(CDlgCompleteManual, CDialog)

CDlgCompleteManual::CDlgCompleteManual(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgCompleteManual::IDD, pParent)
	, m_szNotes(_T(""))
	, m_bElectronic(FALSE)
	, m_dServiceFee(0)
	, m_dCopyrightFee(0)
	, m_dMiscFee(0)
	, m_bDuplicate(FALSE)
	, m_bCanceled(FALSE)
{
	m_nItemID = -1;
	m_nStaffID = 0;
	m_nLibraryID = 0;
	m_nMethodID = 0;
	m_nCollectionID = 0;
	m_bExitOnReturn = FALSE;
	m_pszaMethod = NULL;
	m_pszaCollection = NULL;
	m_bProcessComplete = FALSE;
}

CDlgCompleteManual::~CDlgCompleteManual()
{
}

void CDlgCompleteManual::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_METHOD, m_ctrlMethod);
	DDX_Text(pDX, IDC_EDIT_NOTES, m_szNotes);
	DDX_Control(pDX, IDC_COMBO_COLLECTION, m_ctrlCollection);
	DDX_Check(pDX, IDC_CHECK_ELECTRONIC, m_bElectronic);
	DDX_Text(pDX, IDC_EDIT_FEE_SERVICE, m_dServiceFee);
	DDX_Text(pDX, IDC_EDIT_FEE_COPYRIGHT, m_dCopyrightFee);
	DDX_Text(pDX, IDC_EDIT_FEE_MISC, m_dMiscFee);
	DDX_Check(pDX, IDC_CHECK_DUPLICATE, m_bDuplicate);
	DDX_Check(pDX, IDC_CHECK_CANCELED, m_bCanceled);
}


BEGIN_MESSAGE_MAP(CDlgCompleteManual, CDialog)
	ON_CBN_SELCHANGE(IDC_COMBO_COLLECTION, &CDlgCompleteManual::OnCbnSelchangeComboCollection)
	ON_BN_CLICKED(IDC_CHECK_DUPLICATE, &CDlgCompleteManual::OnBnClickedCheckDuplicate)
END_MESSAGE_MAP()


// CDlgCompleteManual message handlers

BOOL CDlgCompleteManual::OnInitDialog()
{
	CDialog::OnInitDialog();

	ASSERT( m_pszaCollection != NULL );
	ASSERT( m_pszaMethod != NULL );
	
	// get request
	REQUEST_SET Request;
	if( !appConfig.m_SQL.GetRequestSet( &Request, m_nItemID ))
	{
		AfxMessageBox( _T("ERROR getting request.") );
		EndDialog( IDCANCEL );
		return TRUE;
	}
	INT nCurSel = 0;
	INT nSel = 0;
	CString szTemp;

	// get variables
	m_nMethodID = Request.m_method_id;
	m_nCollectionID = Request.m_collection_id;
	m_dServiceFee = Request.m_fee_service;
	m_dCopyrightFee = Request.m_fee_copyright;
	m_dMiscFee = Request.m_fee_misc;

	// check for valid staff
	if( m_nStaffID > 0 )
	{
		// get staff
		STAFF_SET Staff;
		appConfig.m_SQL.GetStaffSet( &Staff, m_nStaffID );
		// override with default
		if( Staff.m_default_collection >= 0 )		
			m_nCollectionID = Staff.m_default_collection;
	}

	// set m_ctrlCollection
	for( INT nIndex = 0; nIndex < m_pszaCollection->GetCount(); nIndex++ )
	{
		szTemp = m_pszaCollection->GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			nSel = m_ctrlCollection.AddString( szTemp );
			m_ctrlCollection.SetItemData( nSel, nIndex );
			if( m_nCollectionID == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlCollection.SetCurSel( nCurSel );

	//m_ctrlMethod
	for( INT nIndex = 0; nIndex < m_pszaMethod->GetCount(); nIndex++ )
	{
		szTemp = m_pszaMethod->GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			nSel = m_ctrlMethod.AddString( szTemp );
			m_ctrlMethod.SetItemData( nSel, nIndex );
			if( m_nMethodID == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlMethod.SetCurSel( nCurSel );

	// check for electronic
	COLLECTION_LOOKUP Collection;
	appConfig.m_SQL.GetDescriptionCollection( &Collection, Request.m_collection_id );
	// set electronc
	m_bElectronic = Collection.nElectronic;

	// show finalcials
	if( appConfig.m_Options.bDisableFinacial )
	{
		GetDlgItem( IDC_STATIC_FEE_SERVICE )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_EDIT_FEE_SERVICE )->ShowWindow( SW_HIDE );

		GetDlgItem( IDC_STATIC_FEE_MISC )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_EDIT_FEE_MISC )->ShowWindow( SW_HIDE );

		GetDlgItem( IDC_STATIC_FEE_COPYRIGHT )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_EDIT_FEE_COPYRIGHT )->ShowWindow( SW_HIDE );
	}

	// set want return
	if( m_bExitOnReturn )
	{
		GetDlgItem( IDC_EDIT_NOTES )->ModifyStyle( ES_WANTRETURN, NULL );
	}

	// update data
	UpdateData( FALSE );

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgCompleteManual::OnOK()
{
	// update data
	UpdateData();

	// get method id
	m_nMethodID = m_ctrlMethod.GetItemData( m_ctrlMethod.GetCurSel() );
	// get collection id
	m_nCollectionID = m_ctrlCollection.GetItemData( m_ctrlCollection.GetCurSel() );

	// get request
	REQUEST_SET Request;
	if( !appConfig.m_SQL.GetRequestSet( &Request, m_nItemID ))
	{
		AfxMessageBox( _T("ERROR getting request.") );
		EndDialog( IDCANCEL );
		return;
	}
	// show message if in processing
	if( Request.m_status_id == STATUS_ID_IN_PROCESS && !m_bProcessComplete )
	{
		if( AfxMessageBox( _T("This request is in processing and may have already been pulled.\n\nDo you want to mark this as complete?"), MB_YESNO ) == IDNO )
			return;
	}

	// return
	CDialog::OnOK();
}

void CDlgCompleteManual::OnCbnSelchangeComboCollection()
{
	// get collection id
	m_nCollectionID = m_ctrlCollection.GetItemData( m_ctrlCollection.GetCurSel() );

	// check for electronic
	COLLECTION_LOOKUP Collection;
	appConfig.m_SQL.GetDescriptionCollection( &Collection, m_nCollectionID );
	// set electronc
	m_bElectronic = Collection.nElectronic;

	// update data
	UpdateData( FALSE );
}

void CDlgCompleteManual::OnBnClickedCheckDuplicate()
{
	// update data
	UpdateData();
	// pre-pend duplictate to note.
	CString szTemp;
	szTemp.Format( _T("DUPLICATE REQUEST: %s"), m_szNotes );
	m_szNotes = szTemp;

	// update data
	UpdateData( FALSE );
}
